package com.pocket.demo.business.service.impl;

import cn.hutool.json.JSONObject;
import com.pocket.demo.base.enums.MsgCodeEnum;
import com.pocket.demo.base.exception.BusinessException;
import com.pocket.demo.business.service.ConfigurationService;
import com.pocket.demo.core.log.LogConstant;
import com.pocket.demo.core.log.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author cookie.
 **/
@Service("ConfigurationService")
public class ConfigurationServiceImpl implements ConfigurationService {

    @Override
    public void updateLogLevel(JSONObject levelConfig) {
        for(LogConstant.LogType logType : LogConstant.LOG_ENUM_LIST){
            String logLevel = levelConfig.getStr(logType.toString());
            if (StringUtils.isBlank(logLevel)) {
                continue;
            }
            logLevel = logLevel.toUpperCase();
            if (!LogConstant.LEVELS.contains(logLevel)) {
                throw new BusinessException(MsgCodeEnum.ERROR_BAD_PARAMETER, new Object[] { logType.toString() });
            }
            // TODO 将 levelConfig 持久化至数据库
            LogUtil.setLevel(logType, logLevel);
        }
    }
}
